package com.distribution.system.controller;

import com.alibaba.fastjson2.JSONObject;
import com.distribution.common.annotation.RepeatSubmit;
import com.distribution.system.domain.bo.ChoosePhoneResultBO;
import com.distribution.system.domain.bo.UnOrderResultBo;
import com.distribution.system.domain.dto.ChoosePhoneDTO;
import com.distribution.system.domain.dto.SendCodeDTO;
import com.distribution.system.domain.dto.PlaceOrderDTO;
import com.distribution.system.domain.req.ReqResult;
import com.distribution.system.service.PlaceOrderOperationService;
import com.distribution.system.utils.http.HttpRequestUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author destinyhlp
 * @date 2023/6/2 04:26
 */
@Slf4j
@RestController
@RequestMapping(value = "/unOrderOperation")
public class PlaceOrderOperationController {
    @Resource
    PlaceOrderOperationService placeOrderOperationService;

    /**
     * 号码查询
     * @param choosePhoneDTO 号码查询参数封装
     * @return 请求状态
     */
    @PostMapping("/getPhone")
    ReqResult getPhone(@RequestBody @Valid ChoosePhoneDTO choosePhoneDTO) {
        ReqResult reqResult = placeOrderOperationService.choosePhone(choosePhoneDTO);
        if (reqResult.getCode() == 200) {
            ChoosePhoneResultBO choosePhoneResultBO = (ChoosePhoneResultBO) reqResult.getData();
            List<String> numArray = choosePhoneResultBO.getNumArray();
            //截取号码列表
            List<String> subList = numArray.subList(0, Math.min(numArray.size(), 15));
            choosePhoneResultBO.setNumArray(subList);
            reqResult.setData(choosePhoneResultBO);
        }
        return reqResult;
//        return ReqResult.error("自定义错误");
    }

    /**
     * 订单提交
     * @param placeOrderDto 订单提交请求参数
     * @return 订单体检返回信息
     */
    @PostMapping("/unOrder")
    @RepeatSubmit()
    ReqResult unOrder(@RequestBody @Valid PlaceOrderDTO placeOrderDto) {
        ReqResult reqResult = placeOrderOperationService.placeOrder(placeOrderDto);
        if (reqResult.getCode() == 200) {
            UnOrderResultBo unOrderResultBo = (UnOrderResultBo) reqResult.getData();
            reqResult.setData(unOrderResultBo.getOrderId());
        }
        return reqResult;
    }

    /**
     * 发送验证码
     * @param sendCodeDTO 订单提交请求参数
     * @return 订单体检返回信息
     */
    @PostMapping("/sendVerificationCode")
    ReqResult sendVerificationCode(@RequestBody @Valid SendCodeDTO sendCodeDTO) {
        return placeOrderOperationService.sendVerificationCode(sendCodeDTO);
    }

    // 高阳互动获取qcjParamStr
    private final String postUrl = "https://dsp-api.19ego.cn/qcj/api/third/landPage/log";
    @PostMapping("/eventPost")
    ReqResult eventPost(@RequestBody String qcjParamStr){
        JSONObject requestJson = new JSONObject();
        requestJson.put("qcjParamStr",qcjParamStr);
        requestJson.put("event",1);
        JSONObject responseJson = JSONObject.parseObject(HttpRequestUtils.sendPost(postUrl, requestJson.toString()));
        if ("000000".equals(responseJson.getString("code"))){
            return ReqResult.success(responseJson.getString("msg"));
        }else {
            return ReqResult.error(responseJson.getString("msg"));
        }
    }

    @PostMapping({"/unOrderSuccessPost"})
    ReqResult unOrderSuccessPost(@RequestBody String qcjParamStr) {
        JSONObject requestJson = new JSONObject();
        requestJson.put("qcjParamStr", qcjParamStr);
        requestJson.put("event", 2);
        JSONObject responseJson = JSONObject.parseObject(HttpRequestUtils.sendPost(this.postUrl, requestJson.toString()));
        if ("000000".equals(responseJson.getString("code"))) {
            return ReqResult.success(responseJson.getString("msg"));
        }
        return ReqResult.error(responseJson.getString("msg"));
    }
}
